package com.qgs.waf.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.stereotype.Service;

import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Funnel;
import com.github.abel533.echarts.series.Pie;
import com.qgs.waf.echarts.ECharts;
import com.qgs.waf.jedis.JedisUtil;
import com.qgs.waf.jedis.RedisKeys;
import com.qgs.waf.service.ZbksComprehensiveSituationService;

/**
 * @show 综合情况,数据从redis拉取
 * @author Administrator
 *
 */
@Service("zbksComprehensiveSituationServiceImpl")
public class ZbksComprehensiveSituationServiceImpl implements ZbksComprehensiveSituationService {

	@Autowired
	private RedisTemplate jedisTemplate;

	@Override
	public Set<TypedTuple> getStatisticsAttacks() {
		return jedisTemplate.opsForZSet().rangeWithScores(RedisKeys.SA, 0, Long.MAX_VALUE);
	}

	/**
	 * @show 攻击饼图
	 * @return
	 */
	@Override
	public String statisticsAttacksPie() {
		String[] pieNames = new String[] { "ddos", "apt", "web", "ips" };
		Set<TypedTuple> attacks = getStatisticsAttacks();
		List<PieData> pieDatas = ECharts.tupleSets2List(attacks, pieNames);
		// 画图
		GsonOption basic = new GsonOption();
		basic.tooltip().trigger(Trigger.item).formatter("{a} <br/>{b} : {c} ({d}%)");
		basic.legend().data(pieNames);
		basic.toolbox().show(true).feature(Tool.dataView, Tool.restore, Tool.saveAsImage, new MagicType(Magic.pie, Magic.funnel).option(new MagicType.Option().funnel(new Funnel().x("25%").width("50%").funnelAlign(X.left).max(1548))));
		Pie pie = new Pie().name("攻击类型（分布）").data(pieDatas.toArray());
		basic.series(pie.center("50%", "45%").radius("50%"));
		return basic.toString();
	}

	@Override
	public String statisticsAttacksByType(String type) {
		return JedisUtil.tupleSets2Json(getStatisticsByType(type),true);
	}

	@Override
	public Set<TypedTuple> getStatisticsByType(String type) {
		return jedisTemplate.opsForZSet().reverseRangeWithScores(RedisKeys.ASS + "." + type, 0, Long.MAX_VALUE);
	}

	@Override
	public String statisticsAttacksList(long size) {
		return JedisUtil.tupleSets2Json(jedisTemplate.opsForZSet().reverseRangeWithScores(RedisKeys.ASL, 0, size),true);
	}

}
